<?php
namespace app\api\controller;
use think\facade\Db;
class menu extends common
{
    private $dbname = 'menu';


    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {

        $list = Db::name($this->dbname)->order('sort desc')->select()->toArray();
        foreach ($list as $k=>$v){
            $list[$k]['meta'] = json_decode($v['meta']);
        }
        return re_success('获取成功',$list);
    }

    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {
        $data = input('post.');
        //模型数据验证
        $model = new \app\api\model\Menu();
        //模型写入
        $data['meta'] = $data['meta']?json_encode($data['meta']):null;
        $data['role'] =  $data['role']?json_encode($data['role']):null;
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        //模型验证

        $data['meta'] = $data['meta']?json_encode($data['meta']):null;
        $data['role'] =  $data['role']?json_encode($data['role']):null;
        $model = new \app\api\model\Menu();
        return $model->edit($data);

    }

    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $info = Db::name($this->dbname)
            ->find(input('get.id'));
        if($info){
            $info['meta'] = $info['meta']?json_decode($info['meta']):[];
            $info['role'] = $info['role']?json_decode($info['role']):[];
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }


    /**
     * 删除
     * @return false|string|void
     */
    public function delete()
    {
        $data = input('delete.id');
        $model = new \app\api\model\Menu();
        return $model->dele($data);
    }
}
